//
//  WDGoutWaitAssignViewController.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/13.
//

#import "WDGoutWaitAssignViewController.h"
#import "WDGoutWaitAssignViewModel.h"
#import "WDGoutWaitAssignTopView.h"
#import "WDCancelWorkOrderBottomView.h"

@interface WDGoutWaitAssignViewController ()
@property (nonatomic, strong) WDGoutWaitAssignViewModel *viewModel;

@property (nonatomic, strong) WDGoutWaitAssignTopView *topView;

@property (nonatomic, strong) WDCancelWorkOrderBottomView *bottomView;

@end

@implementation WDGoutWaitAssignViewController

- (void)dealloc
{
    debugMethod();
}

- (void)initWithVar {
    [super initWithVar];
    
}

- (void)initWithViewModel {
    _viewModel = [WDGoutWaitAssignViewModel new];
    _viewModel.serviceModel = self.serviceModel;
    [_viewModel setupData];
    
}


- (void)initWithNavBar {
    [super initWithNavBar];
    
    [self initWithBackButton];
    self.navigationItem.title = self.viewModel.title;
}

- (void)initWithSubView {
    [super initWithSubView];
    
    [self initWithRootContent:NO];
    self.rootContent.height = self.rootContent.height - TABBAR_HEIGHT;
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    self.rootContent.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    
    [self initWithTopView];
    
    [self initWithBottomView];
}

- (void)initWithTopView {
    if (!_topView) {
        CGRect frame = CGRectMake(0, 0, kScreenW, kWDGoutWaitAssignTopViewHeight);
        _topView = [[WDGoutWaitAssignTopView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_topView];
        @weakify(self);
        _topView.touchCallPhoneBlock = ^{
            @strongify(self);
            [EDSUtility actioinCall:self.viewModel.serviceModel.repairPhone];
        };
        _topView.touchDetailBlock = ^{
            @strongify(self);
            [self actionWithDetail];
        };
        // 跳转到地图页面
        _topView.touchAdsMapBlock = ^{
            @strongify(self);
            [self actionWithCarLocation:self.viewModel.serviceModel];
        };
    }
    [self.topView updateWithViewData:self.viewModel.serviceModel];
}

- (void)initWithBottomView {
    
    if (![[EDSAppDelegateTool sharedInstance] hasPermission:@(10020102) parentCode:@(100201)]) {
        return;
    }
    
    CGRect frame = CGRectMake(0, self.rootContent.bottom, kScreenW, TABBAR_HEIGHT);
    _bottomView = [[WDCancelWorkOrderBottomView alloc] initWithFrame:frame];
    _bottomView.btnTitle = @"抢单";
    [self.view addSubview:_bottomView];
        @weakify(self);
    _bottomView.viewBlock = ^(id  _Nullable data) {
        @strongify(self);
        [self requestWithGrabLock];
    };
}

/// 锁定本单
- (void)requestWithGrabLock {
    [SVProgressHUD showWithStatus:@"锁单中......"];
    @weakify(self);
    [self.viewModel requestWithGrabLock:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
        if (respond.code == EDS_NETWORK_SUCCESS) {
            [SVProgressHUD dismiss];
            [self actionWithRushOrder];
        } else {
//            [SVProgressHUD showInfoWithStatus:@"锁单失败，请稍后再试！"];
            [SVProgressHUD showInfoWithStatus:@"您选择的车辆已被其他救援人员抢单救援"];
        }
    }];
}

/// 车辆定位
/// - Parameter model: model description
- (void)actionWithCarLocation:(WDArrivalServiceItemModel *)model {
    void(^block)(id data) = ^(id data) {
//        @strongify(self);
//        [self doRefreshData];
    };
    
    NSDictionary *params = @{
        @"serviceModel": model,
        @"block":block
    };
    UIViewController *vc = [[TYZRouter sharedInstance] getGoutVehicleLocationVC:params];
    [TYZJumpVCManager pushBaseVC:self pushVC:vc];
}

/// 抢单
- (void)actionWithRushOrder {
    NSMutableDictionary *params = @{@"serviceModel":self.viewModel.serviceModel}.mutableCopy;
    @weakify(self);
    void(^block)(id data) = ^(id data) {
        @strongify(self);
//            [self doRefreshData];
    };
    params[@"block"] = block;
    UIViewController *vc = [[TYZRouter sharedInstance] getGrabOrdersPersonInfoVC:params];
    [TYZJumpVCManager pushBaseVC:self pushVC:vc];
}

/// 工单详情
- (void)actionWithDetail {
    if (![[EDSAppDelegateTool sharedInstance] hasPermission:@(10020101) parentCode:@(100201)]) {
        [SVProgressHUD showInfoWithStatus:@"您没有查看详情的权限！"];
        return;
    }
    
    
    NSMutableDictionary *params = @{@"serviceModel":self.viewModel.serviceModel}.mutableCopy;
    @weakify(self);
    void(^block)(id data) = ^(id data) {
        @strongify(self);
//            [self doRefreshData];
    };
    params[@"block"] = block;
    
    UIViewController *vc = [[TYZRouter sharedInstance] getGoutWaitAssignDetailVC:params];
    [TYZJumpVCManager pushBaseVC:self pushVC:vc];
}




@end
